package com.ynet.eq.haiqing.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 胡超雄
 * @date 2021/6/25 16:50
 * @desc   @ListValue  实现类
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private ListValue listValue;

    @Override
    public void initialize(ListValue listValue) {
        this.listValue = listValue;
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        //不校验非空....空直接放行
        if(value==null){
            return true  ;
        }
        int[] values = listValue.values();
        List<Integer> list = Arrays.stream(values).boxed().collect(Collectors.toList());
        return list.contains(value);
    }

}
